package com.snail.mobile.sim.oss.videoinfo.web.action;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

import com.sungrid.seine.oss.core.setting.model.SysParam;
import com.sungrid.seine.oss.framework.service.BaseManager;
import com.sungrid.seine.oss.framework.util.PageList;
import com.sungrid.seine.oss.framework.web.action.BaseDispatchAction;

import com.snail.mobile.sim.oss.util.Constant;
import com.snail.mobile.sim.oss.util.PicUtil;
import com.snail.mobile.sim.oss.videoinfo.model.TblVideoInfoOss;
import com.snail.mobile.sim.oss.videoinfo.service.TblVideoInfoOssManager;
import com.snail.mobile.sim.oss.videoinfo.web.form.TblVideoInfoOssForm;

/**
 * Action class to handle CRUD on a TblVideoInfoOss object
 * 
 * @struts.action name="tblVideoInfoOssForm"
 *                path="/business/videoinfo/tblVideoInfoOsss" scope="request"
 *                validate="false" parameter="method"
 * @struts.action name="tblVideoInfoOssForm"
 *                path="/business/videoinfo/deleteTblVideoInfoOss"
 *                scope="request" validate="false" parameter="method"
 * @struts.action name="tblVideoInfoOssForm"
 *                path="/business/videoinfo/editTblVideoInfoOss" scope="request"
 *                validate="false" parameter="method"
 * @struts.action name="tblVideoInfoOssForm"
 *                path="/business/videoinfo/saveTblVideoInfoOss" scope="request"
 *                validate="true" parameter="method" input="edit"
 * 
 * @struts.action-forward name="edit" path="tblVideoInfoOss.form"
 * @struts.action-forward name="search" path="tblVideoInfoOss.list"
 * @struts.action-forward name="list" path="tblVideoInfoOsss.do" redirect="true"
 */
public final class TblVideoInfoOssAction extends BaseDispatchAction {

	public ActionForward cancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("list");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'delete' method");
		}

		ActionMessages messages = new ActionMessages();
		TblVideoInfoOssForm tblVideoInfoOssForm = (TblVideoInfoOssForm) form;

		// Exceptions are caught by ActionExceptionHandler
		TblVideoInfoOssManager mgr = (TblVideoInfoOssManager) getBean("tblVideoInfoOssManager");
		mgr.removeTblVideoInfoOss(tblVideoInfoOssForm.getNid());

		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("tblVideoInfoOss.deleted"));

		// save messages in session, so they'll survive the redirect
		saveMessages(request.getSession(), messages);

		return mapping.findForward("list");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'edit' method");
		}

		TblVideoInfoOssForm tblVideoInfoOssForm = (TblVideoInfoOssForm) form;

		// if an id is passed in, look up the user - otherwise
		// don't do anything - user is doing an add
		if (tblVideoInfoOssForm.getNid() != null) {
			TblVideoInfoOssManager mgr = (TblVideoInfoOssManager) getBean("tblVideoInfoOssManager");
			TblVideoInfoOss tblVideoInfoOss = mgr.getTblVideoInfoOss(tblVideoInfoOssForm.getNid());
			tblVideoInfoOssForm = (TblVideoInfoOssForm) convert(tblVideoInfoOss);
			updateFormBean(mapping, request, tblVideoInfoOssForm);
		}

		return mapping.findForward("edit");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'save' method");
		}

		String templatefilepath = createUploadDir();

		// Extract attributes and parameters we will need
		ActionMessages messages = new ActionMessages();
		TblVideoInfoOssForm tblVideoInfoOssForm = (TblVideoInfoOssForm) form;
		boolean isNew = ("".equals(tblVideoInfoOssForm.getNid()) || tblVideoInfoOssForm.getNid() == null);

		TblVideoInfoOssManager mgr = (TblVideoInfoOssManager) getBean("tblVideoInfoOssManager");

		TblVideoInfoOss tblVideoInfoOss = null;

		FormFile file = tblVideoInfoOssForm.getFileuploadpath();

		// add success messages
		if (isNew) {
			tblVideoInfoOss = (TblVideoInfoOss) convertVideoInfoOss(tblVideoInfoOss, tblVideoInfoOssForm);

			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("tblVideoInfoOss.added"));

			// save messages in session to survive a redirect
			saveMessages(request.getSession(), messages);
			
			mgr.saveTblVideoInfoOss(tblVideoInfoOss);

		} else {

			if (file != null && (file.getFileSize() > 0)) {

				tblVideoInfoOss = mgr.getTblVideoInfoOss(tblVideoInfoOssForm.getNid());
				convertVideoInfoOss(tblVideoInfoOss, tblVideoInfoOssForm);

				if (StringUtils.isEmpty(file.getFileName())) {
					tblVideoInfoOss.setSvideoPath(tblVideoInfoOssForm.getSvideoPath());
				} else {

					
					String url =PicUtil.createPic(templatefilepath, file,Constant.VIDEODIR);
					tblVideoInfoOss.setSvideoPath(url);
				}

			} else {

				tblVideoInfoOss = mgr.getTblVideoInfoOss(tblVideoInfoOssForm.getNid());

				convertVideoInfoOss(tblVideoInfoOss, tblVideoInfoOssForm);

				messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("tblVersionInfoOss.updated"));
				saveMessages(request, messages);
			}
			mgr.saveTblVideoInfoOss(tblVideoInfoOss);
		}

		if (isNew) {
			if (file.getFileSize() > 0) {

				String url =PicUtil.createPic(templatefilepath, file,Constant.VIDEODIR);

				tblVideoInfoOss.setSvideoPath(url);
			}
			mgr.saveTblVideoInfoOss(tblVideoInfoOss);
		}

		return mapping.findForward("list");
	}

	

	private TblVideoInfoOss convertVideoInfoOss(TblVideoInfoOss tblVideoInfoOss, TblVideoInfoOssForm tblVideoInfoOssForm) {

		if (null == tblVideoInfoOss) {
			tblVideoInfoOss = new TblVideoInfoOss();
		}

		if (StringUtils.isNotEmpty(tblVideoInfoOssForm.getNid())) {
			tblVideoInfoOss.setNid(Integer.valueOf(tblVideoInfoOssForm.getNid()));
		}

		tblVideoInfoOss.setSdescription(tblVideoInfoOssForm.getSdescription());
		tblVideoInfoOss.setSfileName(tblVideoInfoOssForm.getSfileName());
		tblVideoInfoOss.setNorder(Integer.valueOf(tblVideoInfoOssForm.getNorder()));
		tblVideoInfoOss.setDcreateTime(new Timestamp(new Date().getTime()));
		tblVideoInfoOss.setDupdateTime(new Timestamp(new Date().getTime()));

		return tblVideoInfoOss;

	}

	private String createUploadDir() {

		BaseManager mgr = (BaseManager) getBean("baseManager");
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SysParam.class);
		detachedCriteria.add(Expression.eq("paraName", com.snail.mobile.sim.oss.util.Constant.DEFAULT_APK_PATH));
		SysParam sp;
		List<SysParam> sysParamlst = mgr.findList(detachedCriteria);
		if (CollectionUtils.isNotEmpty(sysParamlst)) {
			sp = (SysParam) sysParamlst.get(0);
			return sp.getParaValue();
		}
		return "";
	}

	public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'search' method");
		}

		// TblVideoInfoOssForm tblVideoInfoOssForm = (TblVideoInfoOssForm) form;
		// TblVideoInfoOss tblVideoInfoOss = (TblVideoInfoOss)
		// convert(tblVideoInfoOssForm);

		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TblVideoInfoOss.class);

		List orders = new ArrayList();

		orders.add(Order.desc("nid"));

		// TblVideoInfoOssManager mgr = (TblVideoInfoOssManager)
		// getBean("tblVideoInfoOssManager");
		BaseManager mgr = (BaseManager) getBean("baseManager");

		PageList pageList = mgr.findPageList(detachedCriteria, getPage(request), orders);

		setPageList(request, pageList);

		return mapping.findForward("search");
	}

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return search(mapping, form, request, response);
	}
}